﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DotNet.Common;
using DotNet.Business;
using DotNet.Model;

using Lucene.Net;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.QueryParsers;
using Lucene.Net.Search;
using Lucene.Net.Store;
using Lucene.Net.Util;
using Lucene.Net.Highlight;
using System.Collections.Generic;
using System.Text;
public partial class Admin_index : basePage
{
    productBll pBll = new productBll();
    ResourceBll resoruceBll = new ResourceBll();
    programClassBll pcBll = new programClassBll();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            TimeSpan stime = DateTime.Now.TimeOfDay;
            TimeSpan etime = DateTime.Now.TimeOfDay;
            TimeSpan t = etime.Subtract(stime);
            runtime.Text = t.TotalMilliseconds.ToString();
        }
    }
    protected void Testbtn_Click(object sender, EventArgs e)
    {
        TimeSpan t1 = DateTime.Now.TimeOfDay;

        for (int i = 1; i <= 100000000; i++)
        {
            i += 1;
        }
        TimeSpan t2 = DateTime.Now.TimeOfDay;
        TimeSpan t3 = t2.Subtract(t1);
        showtest.Text = t3.TotalMilliseconds + "毫秒";
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string reslutStr = "";
        string INDEX_STORE_PATH = Server.MapPath("~\\seacherIndex");  //Index 为索引存储目录
        IndexWriter writer = new IndexWriter(INDEX_STORE_PATH, new StandardAnalyzer(), true);//实例化索引器
        IList<product> list = pBll.GetModel();        
        foreach (product p in list)
        {           
            Document doc = new Document();
            doc.Add(new Field("productId",p.productId.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
            doc.Add(new Field("productName",p.productName, Field.Store.YES, Field.Index.TOKENIZED));  
            doc.Add(new Field("Price", p.Price.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
            doc.Add(new Field("pInfo",p.pInfo, Field.Store.YES, Field.Index.UN_TOKENIZED));
            doc.Add(new Field("pClassId",p.pClassId.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
            doc.Add(new Field("pTitle", p.pTitle.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
            doc.Add(new Field("pKey", p.pKey.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
            doc.Add(new Field("pDescription", p.pDescription.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
            doc.Add(new Field("pButtom", p.pButtom.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
            doc.Add(new Field("pDate",p.pDate.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
            doc.Add(new Field("eDate", p.eDate.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
            doc.Add(new Field("pSourse", p.pSourse.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
            doc.Add(new Field("ptag", p.ptag.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
            doc.Add(new Field("pSort", p.pSort.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
            doc.Add(new Field("productModel",p.Productmodel.ToString(),Field.Store.YES,Field.Index.TOKENIZED));
            doc.Add(new Field("SearchAll",p.pInfo+p.ptag+p.Productmodel+p.productName+p.pDescription, Field.Store.YES, Field.Index.TOKENIZED));
            writer.AddDocument(doc);
        }
        writer.Optimize();
        writer.Close();         
        JsUtil.ShowMsg("产品索引更新成功！！");
        getTechnologyIndex();
    }
    public void getTechnologyIndex() 
    {
        string INDEX_STORE_PATH = Server.MapPath("~\\seacherIndex\\resource");  //Index 为索引存储目录
        IndexWriter writer = new IndexWriter(INDEX_STORE_PATH, new StandardAnalyzer(), true);//实例化索引器
        IList<Resource> list = resoruceBll.GetModel();
        foreach (Resource r in list)
        {
            Document doc = new Document();
            doc.Add(new Field("resourceID",r.ResourceID.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
            doc.Add(new Field("resourceName",r.ResourceName, Field.Store.YES, Field.Index.TOKENIZED));
            doc.Add(new Field("resourceClassID",r.ResourceClassID.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
            doc.Add(new Field("resourceURL",r.ResourceURL, Field.Store.YES, Field.Index.UN_TOKENIZED));
            doc.Add(new Field("resourceDescription",r.resourceDescription, Field.Store.YES, Field.Index.UN_TOKENIZED));
            doc.Add(new Field("resourceDate",r.resourceDate.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));          
            doc.Add(new Field("SearchAll",r.ResourceName+r.resourceDescription, Field.Store.YES, Field.Index.TOKENIZED));
            writer.AddDocument(doc);
        }
        writer.Optimize();
        writer.Close();
        JsUtil.ShowMsg("资源索引更新成功！！");
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        int i = 0;
        int j = 0;
        string path = Server.MapPath("~") + "\\Admin\\website.aspx";
        System.Text.StringBuilder sb = new System.Text.StringBuilder();        
        if (System.IO.File.Exists(path))
        {
            System.IO.StreamReader sr = new System.IO.StreamReader(path);
            string str = sr.ReadToEnd();
            sr.Dispose();
            sb.Append("<div class=\"mianzheng1\">\r");

            #region 产品
            productBll proBll = new productBll();
            productClassBll pClassBll = new productClassBll();
            IList<productClass> pcList = pClassBll.GetModel();
            sb.Append("<div class=\"productlistnav\">产品</div>\r");
            sb.Append("<div class=\"web_list1\">\r");
            foreach (productClass pClass in pcList)
            {
               
                if (i % 3 == 0&&i!=0)
                {
                    sb.Append("</div>\r");
                    sb.Append("<div class=\"web_list1\">\r");
                }
                sb.Append("<div class=\"product_list\">\r");
                sb.AppendFormat("<div class=\"product_list_title\"><a title=\"{0}\" target=\"_blank\" href=\"{1}\">{0}</a></div>\r", pClass.pClassName, UrlTrans.TransUrl("productClass", pClass.pClassId.ToString(), "", "", "", "", ""));
                sb.Append("<div class=\"product_list_neirong\">\r");
                sb.Append(" <ul>\r");
                foreach (product p in pBll.pagingForProduct(1, 100, pClass.pClassId, 0, true))
                {
                    sb.AppendFormat("<li><a target=\"_blank\" title=\"{1}\" href=\"{0}\">{1}</a></li>\r", UrlTrans.TransUrl("productDetail", p.productId.ToString(), "", "", "", "", ""),p.productName);
                }
                sb.Append("</ul>\r");
                sb.Append(" </div>\r"); 
                
                sb.Append("</div>\r");                
                i++;
                
            }
            sb.Append("</div>\r");
            #endregion

            #region 关于特试特
            sb.Append("<div class=\"productlistnav\">关于特试特</div>\r");
            sb.Append("<div class=\"web_list1\">\r");
            sb.Append("<div class=\"web_list\" ><a href=\"copanyprofile.html\" target=\"_blank\">特试特概况</a></div><div class=\"web_list\" ><a href=\"companynews.html\" target=\"_blank\">公司新闻</a></div><div class=\"web_list\" ><a href=\"Culture.html\" target=\"_blank\">公司文化</a></div><div class=\"web_list\" ><a href=\"Management.html\" target=\"_blank\">管理模式</a></div><div class=\"web_list\" ><a href=\"Core.html\" target=\"_blank\">核心能力</a></div><div class=\"web_list\" ><a href=\"companymap.html\" target=\"_blank\">公司地图</a></div><div class=\"web_list\" ><a href=\"service.html\" target=\"_blank\">售后服务</a></div><div class=\"web_list\" ><a href=\"Quality.html\" target=\"_blank\">质量宣言</a></div><div class=\"web_list\" ><a href=\"Qualification.html\" target=\"_blank\">公司资质</a></div><div class=\"web_list\" ><a href=\"Recruitment.html\" target=\"_blank\">诚聘英才</a></div><div class=\"web_list\" ><a href=\"ClientList1.html\" target=\"_blank\">客户名录</a></div><div class=\"web_list\" ><a href=\"wuhanoverview.html\" target=\"_blank\">武汉概况</a></div><div class=\"web_list\" ><a href=\"eatwuhan.html\" target=\"_blank\">吃在武汉</a></div><div class=\"web_list\" ><a href=\"playwuhan.html\" target=\"_blank\">玩在武汉</a></div>");
            sb.Append("</div>\r");
            #endregion           

            #region 成套方案
            DataView dv_program = pcBll.GetDataView(pcBll.GetDataTable(0), 0);           
            sb.Append("<div class=\"productlistnav\">成套方案</div>\r");
            sb.Append("<div class=\"webtao\">\r");
            sb.Append("<ul>\r");
            foreach (DataRowView row in dv_program)
            {
                sb.AppendFormat(" <li><a href=\"{0}\" target=\"_blank\">{1}</a></li>\r", UrlTrans.TransUrl("Completeprogram", row["programClassId"].ToString(), "", "", "", "", ""), row["programClassName"].ToString());
            }
            
            sb.Append("</ul></div>\r");
            #endregion 

            #region 资源
            resourceClassBll rClassBll = new resourceClassBll();
            ResourceBll rBll = new ResourceBll();
            IList<resourceClass> rClassList = rClassBll.GetModel();
            sb.Append("<div class=\"productlistnav\">相关资源</div>\r");
            sb.Append("<div class=\"web_list1\">\r");
            foreach (resourceClass rc in rClassList)
            {

                if (j % 3 == 0 && j != 0)
                {
                    sb.Append("</div>\r");
                    sb.Append("<div class=\"web_list1\">\r");
                }
                sb.Append("<div class=\"product_list\">\r");
                sb.AppendFormat("<div class=\"product_list_title\">{0}</div>\r",rc.ResourceClassName);
                sb.Append("<div class=\"product_list_neirong\">\r");
                sb.Append(" <ul>\r");
                foreach (Resource resource in rBll.ModelByClass(rc.ResourceClassID))
                {
                    sb.AppendFormat("<li><a title=\"{1}\" target=\"_blank\" href=\"..{0}\">{1}</a></li>\r",resource.ResourceURL, resource.ResourceName);
                }
                sb.Append("</ul>\r");
                sb.Append(" </div>\r");
                sb.Append(" </div>\r");
                
                i++;
            }
            sb.Append(" </div>\r");
            #endregion

            sb.Append("</div>\r");

            str =str.Replace("$content$", sb.ToString());
            System.IO.StreamWriter sw = new System.IO.StreamWriter(Server.MapPath("~") + "\\zh\\website.aspx", false, Encoding.GetEncoding("UTF-8"));          
            sw.Write(str);
            sw.Flush();
            sw.Close();
            System.IO.File.Copy(Server.MapPath("~") + "\\Admin\\website.aspx.cs", Server.MapPath("~") + "\\zh\\website.aspx.cs",true);
            JsUtil.ShowMsg("静态页生成成功！");
        }
    }
}
